using System;
using Science.Mathematics;
using L=Science.Physics.GeneralPhysics;

namespace Serway.Chapter02
{
	/// <summary>
	/// Example03: Average and Instantaneous Velocity
	/// A particle moves along the x axis. Its position varies 
	/// with time according to the expression x = -4t +2t^2 
	/// where x is in meters and t is in seconds.
	/// The position-time graph for this motion is shown 
	/// in Figure 2.4. Note that the particle moves in the 
	/// negative x direction for the first second of motion, 
	/// is momentarily at rest at the moment t = 1s, and 
	/// moves in the positive x direction at times t > 1s.
	/// (A) Determine the displacement of the particle in the time 
	/// intervals t = 0 to t = 1 s and t = 1s to t = 3 s.
	/// (B) Calculate the average velocity during these two 
	/// time intervals.
	/// (C) Find the instantaneous velocity of the particle 
	/// at t = 2.5 s.
	/// </summary>
	public class Example03
	{
		public Example03()
		{
		}
		private string result;
		public string Result
		{
			get{return result;}
		}
		public void Compute()
		{
			L.Time t = new L.Time();
			L.Vector.FunctionOfTime PositionTime = new L.Vector.FunctionOfTime(Func);
			L.Position xinitial = new L.Position();
            xinitial.VectorFunctionOfTime = PositionTime;
			L.Position xfinal = new L.Position();
            xfinal.VectorFunctionOfTime = PositionTime;
			L.Position x = new L.Position();
            x.VectorFunctionOfTime = PositionTime;
			
			//(A)
			t.s = 0.0;
			xinitial.Set(t);
			result += Convert.ToString(xinitial.X)+"\r\n";

			t.s = 1.0;
			xfinal.Set(t);
			result += Convert.ToString(xfinal.X)+"\r\n";

			L.Displacement delta = new L.Displacement(xfinal,xinitial);
			result += Convert.ToString(delta.X)+"\r\n";

			t.s = 1.0;
			xinitial.Set(t);
			result += Convert.ToString(xinitial.X)+"\r\n";

			t.s = 3.0;
			xfinal.Set(t);
			result += Convert.ToString(xfinal.X)+"\r\n";

			L.Displacement delta2 = new L.Displacement(xfinal,xinitial);
			result += Convert.ToString(delta2.X)+"\r\n";

			//(B)
			t.Interval = 1.0 - 0.0;
			L.Velocity v1 = new L.Velocity(delta,t);
			result += Convert.ToString(v1.X)+"\r\n";

			t.Interval = 3.0 - 1.0;
			L.Velocity v2 = new L.Velocity(delta2,t);
			result += Convert.ToString(v2.X)+"\r\n";

			//(C)
			t.s = 2.5;
			L.Velocity v = new L.Velocity(x,t);
			result += Convert.ToString(v.X)+"\r\n";
			result += Convert.ToString(v.Y)+"\r\n";
			result += Convert.ToString(v.Z)+"\r\n";
		}
		private L.Vector Func(L.Time t)
		{
			L.Vector x = new L.Vector();
			x.X = -4.0*t.s+2.0*t.s*t.s;
			x.Y = 0.0;
			x.Z = 0.0;
			return x;
		}
	}
}
